package com.warmlight.voicepacket.http;

import android.support.annotation.NonNull;
import android.text.TextUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.TreeMap;

public class HttpStringUtils {

    public static String[] join(String[] s1, String[] s2) {
        if (s1 == null) {
            if (s2 == null) {
                return new String[0];
            } else {
                return s2;
            }
        } else if (s2 == null) {
            return s1;
        } else {
            String[] ss = new String[s1.length + s2.length];
            System.arraycopy(s1, 0, ss, 0, s1.length);
            System.arraycopy(s2, 0, ss, s1.length, s2.length);
            return ss;
        }
    }

    public static int listSize(List<? extends Object> list) {
        if (list != null) {
            return list.size();
        } else {
            return -1;
        }
    }

    public static int arraySize(Object[] array) {
        if (array != null) {
            return array.length;
        } else {
            return -1;
        }
    }

    public static int getItemIndex(Object[] array, Object item) {
        if (item == null) {
            for (int i = 0; i < array.length; i++) {
                if (null == array[i]) {
                    return i;
                }
            }
        } else {
            for (int i = 0; i < array.length; i++) {
                if (item.equals(array[i])) {
                    return i;
                }
            }
        }
        return -1;
    }

    /**
     * @param str
     * @return str==null return-1,else return length
     */
    public static int length(String str, int nullSize) {
        if (str == null) {
            return nullSize;
        } else {
            return str.length();
        }
    }

    /**
     * @param str 输入字符
     * @return str==null return-1,else return length
     */
    public static int lengthTrim(String str, int nullSize) {
        if (str == null) {
            return nullSize;
        } else {
            return str.trim().length();
        }
    }

    /**
     * 切个字符串返回不带空串的list
     */
    public static ArrayList<String> split(String str, String split) {
        ArrayList<String> arrayList = new ArrayList<String>();
        if (str != null) {
            String[] split2 = str.split(split);
            for (String s : split2) {
                if (!TextUtils.isEmpty(s)) {
                    arrayList.add(s);
                }
            }
        }
        return arrayList;
    }

    /**
     * 设置一个list对应角标下的元素为value，如果list大小不够先扩容
     *
     * @param list
     * @param id
     * @param value
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    public static void setList(List list, int id, Object value) {
        if (id >= list.size()) {//先扩容
            list.addAll(Arrays.asList(new Object[id - list.size() + 1]));
        }
        list.set(id, value);//在设置值
    }

    public static int search(String[] array, String value) {
        if (array == null) {
            return -1;
        }
        if (value == null) {
            for (int i = 0; i < array.length; i++) {
                if (array[i] == null) {
                    return i;
                }
            }
        } else {
            for (int i = 0; i < array.length; i++) {
                if (value.equals(array[i])) {
                    return i;
                }
            }
        }
        return -1;
    }

    /**
     * 逗号分割的字符串
     *
     * @param array 要拼接的数组
     * @param split 以什么分割
     * @return string
     */
    public static String splitColon(String[] array, String split) {
        if (array == null) {
            return "";
        }
        StringBuilder builder = new StringBuilder(array.length * 16);
        for (int i = 0; i < array.length; i++) {
            builder.append(array[i]).append(split);
        }
        if (builder.length() > 1 && split != null) {
            return builder.substring(0, builder.length() - split.length());
        }
        return "";
    }

    /**
     * 逗号分割的字符串
     *
     * @param array 要拼接的数组
     * @param split 以什么分割
     * @return string
     */
    public static String splitColon(ArrayList<String> array, String split) {
        if (array == null) {
            return "";
        }
        StringBuilder builder = new StringBuilder(array.size() * 16);
        for (int i = 0; i < array.size(); i++) {
            builder.append(array.get(i)).append(split);
        }
        if (builder.length() > 1 && split != null) {
            return builder.substring(0, builder.length() - split.length());
        }
        return "";
    }

    /**
     * 获取网址的host部分  http://c1.xinstatic.com
     *
     * @param url http://c1.xinstatic.com/che/20160527/1100/5747b85bbc1f4696395.jpg
     * @return
     */
    public static String getHost(String url) {
        if (url != null) {
            int coml = url.indexOf("://");
            int point = url.indexOf(".");
            if (coml > 0 && point > coml) {
                int split = url.indexOf("/", point);
                int splitParam = url.indexOf("?", point);
                if (split > 0) {
                    return url.substring(0, split);
                } else if (splitParam > 0) {
                    return url.substring(0, splitParam);
                } else {
                    return url;
                }
            }
        }
        return "";
    }

    /**
     * 获取网址的uri部分  /che/20160527/1100/5747b85bbc1f4696395.jpg
     *
     * @param url http://c1.xinstatic.com/che/20160527/1100/5747b85bbc1f4696395.jpg
     * @return
     */
    public static String getUri(String url) {
        if (url != null) {
            int coml = url.indexOf("://");
            int point = url.indexOf(".");
            if (coml > 0 && point > coml) {
                int split = url.indexOf("/", point);
                int splitParam = url.indexOf("?", point);
                if (split > 0) {
                    if (splitParam > split) {
                        return url.substring(split, splitParam);
                    } else {
                        return url.substring(split);
                    }
                }
            }
        }
        return "";
    }

    /**
     * 获取get方式的请求参数
     *
     * @param url
     * @return
     */
    public static LinkedHashMap<String, String> getParam(String url) {
        LinkedHashMap<String, String> list = new LinkedHashMap<String, String>();
        int splitParam = url.indexOf("?");
        if (splitParam > 0) {
            String string = url.substring(splitParam + 1);
            ArrayList<String> splitList = split(string, "&");
            for (int i = 0; i < splitList.size(); i++) {
                String[] split = splitList.get(i).split("=");
                if (arraySize(split) == 2) {
                    list.put(split[0], split[1]);
                }
            }
        }
        return list;
    }

    public static StringBuilder getParamEntity(TreeMap<String, Object> params) {
        StringBuilder sb = new StringBuilder();
        if (params != null) {
            for (Entry<String, Object> param : params.entrySet()) {
                Object value = param.getValue();
                String key = param.getKey();
                sb.append(key == null ? "" : key).append("=").append(value == null ? "" : value).append("&");
            }
        }
        return sb;
    }


    @NonNull
    public static StringBuilder getUriParamString(TreeMap<String, String> params) {
        StringBuilder sb;
        if (params != null && params.size() > 0) {
            sb = new StringBuilder(params.size() * 20);
            for (String key : params.keySet()) {
                if (key == null) {
                    continue;
                }
                Object o = params.get(key);
                if (o == null) {
                    o = "";
                }
                String value = o.toString();
                sb.append(key);
                sb.append("=");
                sb.append(value);
                sb.append("&");
            }
            sb.replace(sb.length() - 1, sb.length(), "");
        } else {
            sb = new StringBuilder();
        }
        return sb;
    }

    public static boolean equals(Object o1, Object o2) {
        if (o1 == null) {
            return o2 == null;
        } else {
            return o1.equals(o2);
        }
    }

    public static String trimString(String str) {
        if (TextUtils.isEmpty(str)) {
            return str;
        }
        str = str.replaceAll("\\s*", "");
        return str;
    }
}
